<template>
	<view class="update-container">
		<view class="update-wrapper">
			<view class="input-box">
				<view class="label">
					收货人
				</view>
				<input type="text" v-model="myaddress.buyerName">
			</view>
			<view class="input-box">
				<view class="label">
					联系电话
				</view>
				<input type="text" v-model="myaddress.phone">
			</view>
			<view class="text-box">
				<view class="label">
					收货地址
				</view>
				<textarea v-model="myaddress.buyerAddr"></textarea>
			</view>
		</view>
		
		<view class="btn-box">
			<view class="update" @click="save">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	import { mapMutations, mapState } from 'vuex'
	export default {
		data() {
			return {
				myaddress: {
					buyerName: '',
					phone: '',
					buyerAddr: '',
				}
			}
		},
		methods: {
			...mapMutations('module_user', ['addToMyAddress', 'updateRedirectInfo']),
			save() {
				if(this.myaddress.buyerName === '') return uni.$showMsg('收货人不能为空')
				if(this.myaddress.phone === '') return uni.$showMsg('联系电话不能为空')
				if(this.myaddress.buyerAddr === '') return uni.$showMsg('收货地址不能为空')
				console.log(this.myaddress);
				this.addToMyAddress(JSON.parse(JSON.stringify(this.myaddress)))
				let pages = getCurrentPages()
				let beforePage = pages[pages.length - 2]; // 上一页
				uni.navigateBack({
				    success: () => {
				        beforePage.onLoad(); // 执行上一页的onLoad方法
				    }
				})
			},
		},
	}
</script>

<style lang="scss">
.update-container {
	width: 100%;
	.update-wrapper {
		width: 100%;
		padding: 20rpx;
		background-color: #fff;
		box-sizing: border-box;
	}
	.input-box {
		display: flex;
		height: 40px;
		align-items: center;
		border-bottom: #f0f0f0 1px solid;
		.label {
			margin-right: 20rpx;
			width: 80px;
			font-weight: bold;
		}
		input {
			flex-grow: 1;
			height: 40px;
		}
	}
	.text-box {
		margin-top: 20rpx;
		.label {
			font-weight: bold;
		}
	}
	textarea {
		width: 100%;
		height: 100px;
		margin-top: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		border: 1px solid #f0f0f0;
	}
	.btn-box {
		padding: 30rpx;
		.update {
			width: 100%;
			color: #fff;
			height: 40px;
			line-height: 40px;
			text-align: center;
			background-color: aqua;
		}
	}
}
</style>
